/*------------------------------------------------------------------------------
 * PROJ : PrototypeWithSpring
 * NAME : sculove.test.xmlParser.KeicXrwParser.java
 *        
 * Copyright 2009 sculove All rights reserved
 *------------------------------------------------------------------------------
*/
package sculove.test.parser.xml;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import jxl.CellView;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import sculove.spring.framework.util.SStringUtil;
import sun.util.logging.resources.logging;


/**
 * <pre>
 * sculove.test.xmlParser.KeicXrwParser.java
 * TODO
 * 
 * </pre>
 * @date : 2009. 9. 25. ���� 3:19:28
 * @version :
 * @author : sculove
 */
public class KeicXrwParser {
    private Log log = LogFactory.getLog(this.getClass());
    private SAXParser parser;
    private HashMap<String, ArrayList> mapData;
    
    public KeicXrwParser() {
        try{
            mapData = new HashMap<String, ArrayList>();
            // SAX Parser Factory ��
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(true);

            // SAX Parser ��
            this.parser = factory.newSAXParser();            
        }catch(Exception e){
            log.error("Fail! create SAX-Parser");
        }
    }
    
    private void parse(File file, DefaultHandler handler) {
        try{
            parser.parse(file, handler);
        }catch(SAXException e){
            log.error(" �� " + file.getAbsolutePath() + " : " + e.getMessage());
//            e.printStackTrace();
        }catch(IOException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void parsingDoc(File target) {
        if(target.isDirectory()) {    // Directory�� ���
            String[] child = target.list();
            for(int i = 0; i < child.length; i++){
                parsingDoc(new File( target.getAbsolutePath() + "\\" + child[i]));
            }
        } else {    // File�� ���
            if("xrw".equalsIgnoreCase(SStringUtil.unqualify(target.getAbsolutePath(), '.')) 
                    && (target.getAbsolutePath().indexOf("$") == -1)
                    && (target.getAbsolutePath().indexOf("keic\\xrw\\com\\example") == -1)
                    && (target.getAbsolutePath().indexOf("keic\\xrw\\dev") == -1)
                    && (target.getAbsolutePath().indexOf("keic\\xrw\\zpr") == -1)
            ) {
                ArrayList list = new ArrayList(); 
                parse(target.getAbsoluteFile(), new KeicXrwHandler(list));
                mapData.put(target.getAbsolutePath(), list);
            }
        }
    }
    
    private void writeExcel(String filepath) {
        WritableWorkbook workbook = null;
        WritableSheet sheet = null;
        File excelFile = new File(filepath);
        
        try{
            workbook = Workbook.createWorkbook(excelFile);
            sheet = workbook.createSheet("���α׷����-ȭ��", 0);
            
            // �÷� Auto size ����
            CellView autoSizeCellView = new CellView();
            autoSizeCellView.setAutosize(true);
            sheet.setColumnView(0, autoSizeCellView);
            sheet.setColumnView(1, autoSizeCellView);
            sheet.setColumnView(2, autoSizeCellView);
            sheet.setColumnView(3, autoSizeCellView);
            sheet.setColumnView(4, autoSizeCellView);
            sheet.setColumnView(5, autoSizeCellView);
            sheet.setColumnView(6, autoSizeCellView);
            
            sheet.addCell(new Label(0,0,"������"));
            sheet.addCell(new Label(1,0,"���ϸ�"));
            sheet.addCell(new Label(2,0,"Application"));            
            sheet.addCell(new Label(3,0,"SO"));
            sheet.addCell(new Label(4,0,"Operation"));
            sheet.addCell(new Label(5,0,"Input DTO"));
            sheet.addCell(new Label(6,0,"Output DTO"));

            int row = 1;
            Iterator<String> iter = mapData.keySet().iterator();
            while(iter.hasNext()) {
                String key = iter.next();
                ArrayList list = mapData.get(key);

                String appNm = key.substring(key.indexOf("keic\\xrw")+9, key.indexOf("keic\\xrw")+12);
                if(appNm.equals("Keic")) appNm="����"; 
                else if(appNm.equals("itc")) appNm="ITSM";
                else if(appNm.equals("mac")) appNm="ȸ��";
                else if(appNm.equals("mhc")) appNm="�λ�";
                else if(appNm.equals("muc")) appNm="��������";
                else if(appNm.equals("rac")) appNm="�μ�";
                else if(appNm.equals("rcc")) appNm="����";
                else if(appNm.equals("rec")) appNm="���̹����⺸����";
                else if(appNm.equals("ric")) appNm="�ſ�����";
                else if(appNm.equals("rmc")) appNm="��ǰ";
                else if(appNm.equals("rrc")) appNm="���İ�";
                else if(appNm.equals("rsc")) appNm="�?����";
                else if(appNm.equals("ruc")) appNm="��������";
                else if(appNm.equals("zzz")) appNm="�������";

                sheet.addCell(new Label(0,row,appNm));
                sheet.addCell(new Label(1,row,key.substring(key.indexOf("keic\\xrw"))));
                sheet.addCell(new Label(2,row,key.substring(key.indexOf("keic\\xrw")+9, key.indexOf("keic\\xrw")+12)));
                row++;                 
                
                if(list.size() ==0 ) {
                    sheet.addCell(new Label(0,row,appNm));
                    sheet.addCell(new Label(1,row,key.substring(key.indexOf("keic\\xrw"))));
                    sheet.addCell(new Label(2,row,key.substring(key.indexOf("keic\\xrw")+9, key.indexOf("keic\\xrw")+12)));
                    row++;                    
                } else {
                	for(int i=0; i<list.size(); i++) {
	                    HashMap data = (HashMap)list.get(i);
	                    sheet.addCell(new Label(0,row,appNm));
	                    sheet.addCell(new Label(1,row,key.substring(key.indexOf("keic\\xrw"))));
	                    sheet.addCell(new Label(2,row,(String)data.get("APP")));
	                    sheet.addCell(new Label(3,row,(String)data.get("SO")));
	                    sheet.addCell(new Label(4,row,(String)data.get("OP")));
	                    sheet.addCell(new Label(5,row,(String)data.get("INDTO")));
	                    sheet.addCell(new Label(6,row,(String)data.get("OUTDTO")));
	                    row++;
	                }
                }
            }
        }catch(Exception e){
           e.printStackTrace();
        } finally {
            try{
                if(workbook != null) workbook.write();
                if(workbook != null) workbook.close();   
            }catch(Exception e2){}
        }
        
    }
    
    /**
     * XRW �Ľ� ���
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        KeicXrwParser parser = new KeicXrwParser();
        File baseDir = new File("E:/keicDev/view/sculove_view/KEIC_SRC/keic/xrw");
        System.out.println("--Start to parse document...");
        if(baseDir.isDirectory()) {
            parser.parsingDoc(baseDir);
        }
        System.out.println("--End to parse document...");                
        System.out.println("--Start to write excel...");
        parser.writeExcel("C:/���α׷����.xls");
        System.out.println("--End to write excel...");
        
        /*
         PBC ���� ����
          [ ���� DB ]
          - ���� : proframe_mon
          - PW : promon786

            SELECT DISTINCT
                   case 
                    when substr(B.Resource_Path,6,3) = 'com' then '����'                
                    when substr(B.Resource_Path,6,3) = 'itc' then 'ITSM'                                           
                    when substr(B.Resource_Path,6,3) = 'mac' then 'ȸ��'               
                    when substr(B.Resource_Path,6,3) = 'mhc' then '�λ�'               
                    when substr(B.Resource_Path,6,3) = 'muc' then '��������'           
                    when substr(B.Resource_Path,6,3) = 'rac' then '�μ�'              
                    when substr(B.Resource_Path,6,3) = 'rcc' then '����'              
                    when substr(B.Resource_Path,6,3) = 'rec' then '���̹����⺸����' 
                    when substr(B.Resource_Path,6,3) = 'ric' then '�ſ�����'           
                    when substr(B.Resource_Path,6,3) = 'rmc' then '��ǰ'               
                    when substr(B.Resource_Path,6,3) = 'rrc' then '���İ�'           
                    when substr(B.Resource_Path,6,3) = 'rsc' then '�?����'           
                    when substr(B.Resource_Path,6,3) = 'ruc' then '��������'           
                    when substr(B.Resource_Path,6,3) = 'zzz' then '�������'           
                    end as "������", 
                   substr(B.Resource_Path,6,3) "Application",
                   B.Resource_Path "PBC", 
                   B.Operation_Name "Operation"
            FROM proframe.dev_resource A, proframe.dev_operation_dep B
            WHERE (
                  A.resource_path like 'keic/com%PBC%'
                   or A.resource_path like 'keic/itc%PBC%'
                   or A.resource_path like 'keic/mac%PBC%'
                   or A.resource_path like 'keic/mhc%PBC%'       
                   or A.resource_path like 'keic/muc%PBC%'       
                   or A.resource_path like 'keic/rac%PBC%'       
                   or A.resource_path like 'keic/rcc%PBC%'       
                   or A.resource_path like 'keic/ruc%PBC%'       
                   or A.resource_path like 'keic/rec%PBC%'       
                   or A.resource_path like 'keic/ric%PBC%'       
                   or A.resource_path like 'keic/rmc%PBC%'       
                   or A.resource_path like 'keic/rrc%PBC%'       
                   or A.resource_path like 'keic/rsc%PBC%'       
                   or A.resource_path like 'keic/ruc%PBC%'       
                   or A.resource_path like 'keic/zzz%PBC%'       
                   )
                  and resource_div = 'general'
                  and A.resource_path = B.resource_path
            ORDER BY B.RESOURCE_PATH;
         */
    }
}
